import React from 'react'
import { Form, Input, Tooltip, Icon, Cascader, Select, Row, Col, Checkbox, Button ,Radio,Modal,Message} from 'antd';
import api from '../../../api/area';
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
const { TextArea } = Input;



import PanelBox from '../../../components/PanelBox';
import Antdicons from '../Antdicon';

import './index.less'

class EditForm extends React.Component {

  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        if(values.id){
          this.update(values);
        }else{
          this.add(values);
        }
       
      }
    });
  }
  update= (values) =>{
    api.put('/updateArea', {...values}).then((data) => {
      const result= data.data;
     if(result.code == 0){
      Message.success('修改成功！');
      const { close,refeshData } = this.props;
      refeshData();
      close();
     }else{
      Message.error(result.message);
     }
    });
  }
  add= (values) =>{
    values.parentIds=values.parentIds+values.parentId+',';
    api.post('/addArea', {...values}).then((data) => {
      const result= data.data;
     if(result.code == 0){
      Message.success('添加成功！');
      const { close,refeshData } = this.props;
      refeshData();
      close();
     }else{
      Message.error(result.message);
     }
    });
  }


  render() {
    const { getFieldDecorator,resetFields } = this.props.form;
    const { close ,callbackParent} = this.props;
    callbackParent({
      resetFields:resetFields,
    });

    const record = this.props.rowRecord;
    const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };
    const tailFormItemLayout = {
      wrapperCol: {
        xs: {
          span: 14,
          offset: 0,
        },
        sm: {
          span: 14,
          offset: 6,
        },
      },
    };
    
    return (
      <div>
      <Form onSubmit={this.handleSubmit}>
       
          {getFieldDecorator('id', {
            initialValue:record ? record.id : ''
          })(
            <Input  type="hidden" />
          )}
         {/*
          {getFieldDecorator('parentIds', {
            initialValue:record ? record.parentIds : '0,1'
          })(
            <Input  type="hidden" />
          )}
        */}
          {getFieldDecorator('parentId', {
            initialValue:record ? record.parentId : '1'
          })(
            <Input  type="hidden" />
          )}
        
        <FormItem
          {...formItemLayout}
          label="上级菜单"
        >
            {getFieldDecorator('parentName', {
                initialValue:record ? record.parentName : '中国'
              })(
              <Input  disabled />
           )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="名称"
          hasFeedback
        >
          {getFieldDecorator('name', {
            rules: [{ required: true, message: '名称不能为空!' }],
            initialValue:record ? record.name : ''
          })(
            <Input />
          )}
        </FormItem>
        
        <FormItem
          {...formItemLayout}
          label="排序"
        >
          {getFieldDecorator('sort', {
            initialValue:record ? record.sort : ''
          })(
            <Input placeholder ="如不填排序号，则自动排到该等级末尾！"/>
          )}
        </FormItem> 
       
        {/*
        <FormItem
          {...formItemLayout}
          label="类型:"
        >
          {getFieldDecorator('type', {
            initialValue:record ? record.type : ''
          })(
              <Select>
                <Option value="1">国家</Option>
                <Option value="2">省份,直辖市</Option>
                <Option value="3">地市</Option>
                <Option value="4">区县</Option>
                <Option value="5">街道</Option>
              </Select>
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="备注:"
        >
          {getFieldDecorator('remarks', {
            initialValue:record ? record.remarks : ''
          })(
            <TextArea rows={4} />
          )}
        </FormItem>*/}
        <FormItem {...tailFormItemLayout}>
          <Button type="primary" htmlType="submit" size="large" style={{marginRight:'10px'}}>提交</Button>
          <Button onClick={close}>取消</Button>
          
        </FormItem>
      </Form>
       </div>
    );
  }
}

const WrappedEditForm = Form.create()(EditForm);
export default WrappedEditForm
